import { defineStore } from 'pinia'

import {
  postPageListData,
  deletePageDataById,
  createPageListData,
  editPageListData
} from '@/service/main/system'

import useMainStore from '@/store/main/main'

import type { userListData } from '@/types/index'

interface IStateType {
  userData: userListData
  pageTotalCount: number
  pageList: any[]
}

const mainStore = useMainStore()

const useStytem = defineStore('system', {
  state: (): IStateType => ({
    userData: { totalCount: 0, list: [] },
    pageTotalCount: 0,
    pageList: []
  }),
  actions: {
    // 请求用户列表

    // 针对页面的增删改查
    async postPageListAction(
      pageName: string,
      queryInfo: { offset: number; size: number } = { offset: 0, size: 10 }
    ) {
      const result = await postPageListData(pageName, queryInfo)
      const { totalCount, list } = result.data
      this.pageTotalCount = totalCount
      this.pageList = list
    },
    async deletePageByIdAction(pageName: string, id: number) {
      const result = await deletePageDataById(pageName, id)

      // 重新请求部门列表,角色列表以及页面列表数据
      mainStore.fetchEntireDataAction()
      this.postPageListAction(pageName)
      return result
    },
    async createPageDataListAction(pageName: string, dataInfo: any) {
      const result = await createPageListData(pageName, dataInfo)

      // 重新请求部门列表,角色列表以及页面列表数据
      mainStore.fetchEntireDataAction()
      this.postPageListAction(pageName)
      return result
    },
    async editPageDataListAction(pageName: string, id: number, userInfo: any) {
      const result = await editPageListData(pageName, id, userInfo)

      // 重新请求部门列表,角色列表以及页面列表数据
      mainStore.fetchEntireDataAction()
      this.postPageListAction(pageName)
      return result
    }
  }
})
export default useStytem
